package com.movie2.controller;

import com.movie2.config.MyConstants;
import com.movie2.model.entity.*;
import com.movie2.model.vo.OrderVo;
import com.movie2.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;
    @Autowired
    SeatService seatService;
    @Autowired
    ArrangementService arrangementService;
    @Autowired
    FilmService filmService;

    @GetMapping("")
    public List<Map<String,Object>> getAllOrderByUid(@RequestParam("uid") int uid){
        List<Map<String,Object>> mapList=new ArrayList<>();
                // <!-- 等待支付0 订单超时 1 支付成功2  订单异常3 -->
        Order temp=new Order();
        temp.setUid(uid);
        List<Order> orders = orderService.selectOrdersByCondition(temp);
        for (Order order : orders) {
            Arrangement arrangement = arrangementService.selectByPrimaryKey(order.getAid());
            Film film = filmService.selectByPrimaryKey(arrangement.getFid());
            List<Map<String, Object>> lineAndRow = NumToLineAndRow(order.getSeats());
            Map<String,Object> map=new HashMap<>();
            map.put("arrangement",arrangement);
            film.setCover(MyConstants.MY_URL+film.getCover());
            map.put("film",film);
            map.put("seats",lineAndRow);
            map.put("order",order);
            mapList.add(map);

        }
        return mapList;
    }

    @PostMapping("/add")
    public Map<String,Object> add(@RequestBody OrderVo vo){
        // ids 所有要付款的购物车的id，修改状态
        log.info("OrderController -> add(@RequestBody int[] ids)");
        // 通过ID获得购物车项，获得所有的座位信息
        // System.out.println(vo.getIds());
        System.out.println(vo);
        List<Cart> carts=cartService.selectAllCartsByIds(vo.getIds());
        Map<String,Object> result=new HashMap<>();
        for (Cart cart : carts) {
            StringBuffer sb=new StringBuffer();
            sb.append(cart.getSeats());
            sb.append(",");
            String seats=sb.toString();
            seats=seats.substring(0,seats.length()-1);
            // 修改选中的购物车项的状态，0-》2，foreach

            Map<String,Object> map=new HashMap<>();
            map.put("status",2);
            map.put("array",vo.getIds());
            int count=cartService.updateStatusByIds(map);

            // 添加一个订单项
            Order order=new Order();
            order.setAid(cart.getAid());
            order.setPhone(cart.getPhone());
            order.setPrice(cart.getPrice());
            order.setStatus(2);
            order.setUid(cart.getUid());
            order.setSeats(seats);
            count=orderService.insert(order);


            // 批量添加座位信息
            List<Seat> seatList=new ArrayList<>();
            String[] mySeats=seats.split(",");
            for (String mySeat : mySeats) {
                Seat s=new Seat();
                s.setAid(cart.getAid());
                s.setSeat(Integer.parseInt(mySeat));
                seatList.add(s);
            }
            count=seatService.insertList(seatList);
        }

        result.put("success","ok");
        return result;
    }

    public List<Map<String,Object>> NumToLineAndRow(String str){
        List<Map<String,Object>> mapList=new ArrayList<>();
        String [] tag=str.split(",");
        for (String s : tag) {
            Map<String,Object> map=new HashMap<>();
            int num=Integer.parseInt(s);
            int line= (int)Math.ceil((double) num/12);

            int row=0;
            if(num%12==0){
                row=12;
            }else{
                row=(int) num%12;
            }
            // System.out.println(num);
            // System.out.println(line);
            // System.out.println(row);
            // System.out.println("==========");
            map.put("line",line);
            map.put("row",row);
            mapList.add(map);
        }
        return mapList;
    }
}
